<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<%@ page import="org.mebusw.struts.vo.GameTable"%>
<%@ page import="org.mebusw.dao.factory.DAOFactory"%>
<%@ page import="org.mebusw.dao.IGameTableDAO"%>
<%@ page import="org.mebusw.struts.davinci.bean.GameBrief"%>
<%
	String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
  <head>
    <html:base />
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="Content-Type" content="text/html; GBK">
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  <script  language="javascript"  src='./js/data.js'></script>
  </head>
  
  <body>
   
    <H1>Welcome to Davinci Code Online Portal.  </H1><br>
    <INPUT id="calendar" onclick='popUpCalendar(this, this, " mm/dd/yyyy")' readonly="readonly" type="text"  size="11" name="Text1">
    <br/>
    <%  
        boolean isLoggedIn = false;
        Cookie c[] = request.getCookies();
        Cookie unameCookie;
        String cookieValue;
        if (null != c)   {
          for (int i = 0; i < c.length ; i++)  {
             if (c[i].getName().equals("unameCookie"))   {
                unameCookie = c[i];
                cookieValue = unameCookie.getValue();
                out.println(cookieValue + " has logged in. (cookie)");
    %>
        <jsp:useBean id="userNameBean" class="org.mebusw.struts.davinci.bean.UserName" scope="session"></jsp:useBean>
        <jsp:setProperty property="userName" name="userNameBean" value="<%=cookieValue %>"/> 
    <%
                isLoggedIn = true;
                break;
                }
            }
        }
        if (isLoggedIn) {
           out.write("<a href='./login.jsp'>Switch User</a>");            
        }  
        else{
           out.write("<a href='./login.jsp'>Log In Now</a>");
        }
     %>  
    
	<a href="./create.jsp">Create a new game</a><br>
	
    <br>
	<table border="1">
	<tr><th colspan="6">Game List</th></tr>
    <%      
        IGameTableDAO dao = DAOFactory.getIGameTableDAOInstance();
        List<GameBrief> gameList = dao.findAll();
        pageContext.setAttribute("gameList", gameList, PageContext.PAGE_SCOPE); 
    %>  
	<logic:iterate id="item" name="gameList" indexId="index">
	    <tr>
	    <td><bean:write name="index" /></td>
        <td><a href='./play.jsp?gid=<bean:write name="item" property="gid"/>'>
            Play #<bean:write name="item" property="gid"/> Now</a></td>
        <td><bean:write name="item" property="description"/></td>
        <td><bean:write name="item" property="players"/></td>
        <td><bean:write name="item" property="gameStatus"/></td>
        <td><bean:write name="item" property="playerCount"/></td>
        </tr>
	</logic:iterate> 
     
    </table>
    <h2>$Rev: 36 $</h2>
    
  </body>
</html:html>